package com.chzc.fm.duotin.ui.home;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.chzc.fm.duotin.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
public class Graduate extends View {

    //判断何时画长线
    private int longLine = 0;
    //View前点的坐标
    private float X = 0;
    private float Y = 0;
    //View之前的位置
    private int lastX = 0;



    public void setTextList(List<String> textList) {
        this.textList = textList;
        //设置为wrap_content及AT_MOST
       // this.measure(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        //神来之笔
      /*  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(params);*/
    }

    private int lastY = 0;

    private List<String> textList;

    private List<TextRect> list = new ArrayList<>();
    //刻度尺的间距
    private int size = 20;

    //刻度文字的数据结构的封装
    private class TextRect{
        String hello;
        //所控制区域的起始点坐标
     public   int fromX;
     public    int fromY;
        //所控制区域的末尾点坐标
     public    int endX;
     public    int endY ;
        //用一个矩形对象保存所对应的区域
        Rect rect;

        public TextRect(String string ,int fromX,int fromY,int endX,int endY ){
            this.hello = string;
            this.fromX = fromX;
            this.fromY = fromY;
            this.endX = endX;
            this.endY = endY;

            rect = new Rect(fromX,fromY,endX,endY);

        }

        //判断方块是否包含某一点
        boolean include(float x,float y){

            return (x>fromX&&x<endX)&&(y>fromY&&y<endY);
        }

    }

    public void setOnItemClickListener(IOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private IOnItemClickListener onItemClickListener;

    public Graduate(Context context) {
        super(context);

    }

    public Graduate(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {

       // this.measure(0,0);

        Log.e("shuxu","onDraw");
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setStrokeWidth(5);
        size = 20;
        //先画两条
       // String[] titles = new String[]{"推荐","新闻"};
        if (textList==null||textList.size()<18){
            return;
        }

        TextPaint textPaint = new TextPaint();
        textPaint.setStrokeWidth(2);
        textPaint.setTextSize(30);

        Log.e("count:"," =" + textList.size()*5);
        //以下用来标记索引的值
        int j = 0;
        for(int i =0;i<textList.size()*5;i++){
            if (i%5==2) {

             canvas.drawText(textList.get(j), size *(i-1)-8,30,textPaint);
                //判断画了几个文字
                //保存文字所处的区域
                TextRect text = new TextRect(textList.get(j),0,0,0,0);
                list.add(text);

                canvas.drawLine(size *i,50, size *i,100,paint);
                //索引加1
                j=j+1;
                continue;
            }
            canvas.drawLine(size *i,60, size *i,100,paint);
        }
        //  int bottom = getResources().getDisplayMetrics().heightPixels;
        int bottom = getWidth();
        canvas.drawLine(0,150,bottom,150,paint);
       //接受传过来的值
      //   canvas.rotate(90);
    }
    //触摸事件的响应范围仅限于该View的区域

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //获取手指落下的坐标并保存
                X =  event.getX();
                Y =  event.getY();

                post(new Runnable() {
                    @Override
                    public void run() {
                        //计算每一个索引所处的区域
                      //  int size = getWidth()/list.size();
                        //索引区域的高度
                        int width = size*5;
                        LogUtil.e("Width:"+width);
                        //索引区域的高度
                        int height = getHeight();
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).fromX =width*i;
                            //计算中止点的x轴坐标
                            list.get(i).endX = width*(i+1);
                            list.get(i).endY = height;
                            //LogUtil.e("height+widht"+":"+getHeight()+":"+list.get(i).fromX);
                        }
                        //LogUtil.e("执行了");
                    }
                });
                //一定要返回true
                return true;
            case MotionEvent.ACTION_UP:
                //执行回调方法
                if (onItemClickListener!=null){
                    //传递索引
                    onItemClickListener.onItemClick(getClickPosition(X,Y));

                }
                LogUtil.e("position:"+getClickPosition(X,Y));
                break;

        }

        return super.onTouchEvent(event);

    }

    private int getClickPosition(float x, float y) {
        //判断x,y所点击的区域
        // LogUtil.e("list:"+);
        for (int i = 0; i <list.size() ; i++) {
            //判断该点的位置
            if (list.get(i).include(x,y)) {
               // LogUtil.e("这里执行了几次"+i);
                return i;
            }
        }
        return 0;
    }
    public interface IOnItemClickListener{
        /*
        * 所在位置的索引
        * */
        public void onItemClick(int position);
    }
    //让
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(setWidthMeasureSpec(widthMeasureSpec),setHeightMeasureSpec(heightMeasureSpec));
    }

    private int setHeightMeasureSpec(int heightMeasureSpec) {
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        int result = 0;
        if (specMode==MeasureSpec.EXACTLY){
            result = specSize;
        }else {
            result = 150;
            if (specMode==MeasureSpec.AT_MOST){
                result = Math.min(specSize,result);
            }
        }
        return result;
    }

    private int setWidthMeasureSpec(int widthMeasureSpec) {

       // LogUtil.e("看看调用了几次--");

        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        int result = 0;
        if (specMode==MeasureSpec.EXACTLY){
            result = specSize;
        }else {
            //获取宽高
            if (textList!=null){

              int wrapResult = size*5*textList.size();
         //       LogUtil.e("宽度"+wrapResult+"长度"+textList.size()+"间距"+size);
                if (specMode==MeasureSpec.AT_MOST){
                   // wrapResult = Math.min(specSize,result);
                }

                return wrapResult;
            }

        }
        return result;
    }
}
